
var EventEmitter = require('events').EventEmitter;

// domain module, handle global exception event, 
var domain = require('domain');

var emitter = new EventEmitter();

var domain1 = domain.create();

domain1.on('error', err => {console.log('domain1 handle error', err.message);});

// explicit bind
domain1.add(emitter);

emitter.on('error', err => {console.log('Listener handle error', err.message);});


emitter.emit('error', new Error('handle by listener'));

emitter.removeAllListeners('error');

emitter.emit('error', new Error('handle by domain1'));

var domain2 = domain.create();

domain2.on('error', err => {console.log('domain2 handle error', err.message);});

// implicit bind
domain2.run(() => {
	var emitter_local = new EventEmitter();
	emitter_local.emit('error', new Error('handle by domain2'));
});

domain1.remove(emitter);

emitter.emit('error', new Error('error system will exit'));

